package th.co.bjc.md.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import th.co.bjc.md.web.bean.Expertise;
import th.co.bjc.md.web.bean.Manufacturer;
import th.co.bjc.md.web.bean.Product;
import th.co.bjc.md.web.bean.ProductImg;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class ProductServlet
 */
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("ACTION");
		if(action == null){
			request.setAttribute("expertiseId", request.getParameter("expertiseId"));
			getServletContext().getRequestDispatcher("/jsp/product.jsp").forward(request, response);
		}else if(action.equals("GETMANUFACTURER")){
			getManufacturer(request, response);
		}else if(action.equals("GETPRODUCTLIST")){
			getProductList(request, response);
		}else if(action.equals("GETPRODUCTDETAIL")){
			getProductDetail(request, response);
		}
		
	}
	
	private void getManufacturer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int expertiseId = 0;
		if(!request.getParameter("expertiseId").equalsIgnoreCase("null")){
			expertiseId = Integer.parseInt(request.getParameter("expertiseId"));
		}
		PrintWriter out = response.getWriter();
		ArrayList list = new ArrayList();
		
		if(expertiseId != 0){
			Expertise e1 = new Expertise();
			e1.setExpertiseId(1);
			e1.setExpertiseName("TEST1");
			e1.setRegion("NORTH");
			list.add(e1);
		}
		Manufacturer m1 = new Manufacturer();
		m1.setManufacturerId(1);
		m1.setManufacturerName("Pepsi");
		m1.setLogoName("pepsi.jpg");
		list.add(m1);
		
		Manufacturer m2 = new Manufacturer();
		m2.setManufacturerId(2);
		m2.setManufacturerName("Cola");
		m2.setLogoName("cola.jpg");
		list.add(m2);
		
		Manufacturer m3 = new Manufacturer();
		m3.setManufacturerId(3);
		m3.setManufacturerName("Est");
		m3.setLogoName("est.jpg");
		list.add(m3);
		
		Manufacturer m4 = new Manufacturer();
		m4.setManufacturerId(4);
		m4.setManufacturerName("M150");
		m4.setLogoName("m150.jpg");
		list.add(m4);
		
		Manufacturer m5 = new Manufacturer();
		m5.setManufacturerId(5);
		m5.setManufacturerName("OICHI");
		m5.setLogoName("m150.jpg");
		list.add(m4);
		
		JSONArray jsonObject = JSONArray.fromObject(list);
		out.print(jsonObject);
	}
	
	private void getProductList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		ArrayList list = new ArrayList();
		
		Manufacturer m1 = new Manufacturer();
		m1.setManufacturerId(1);
		m1.setManufacturerName("Pepsi");
		m1.setLogoName("pepsi.jpg");
		list.add(m1);
		
		Product p1 = new Product();
		p1.setProductId(1);
		p1.setProductName("Orange Juice");
		p1.setProductDetail("Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!Good test!");
		
		ProductImg pip1 = new ProductImg();
		pip1.setImgId(1);
		pip1.setImgName("orange1.jpg");
		pip1.setImgDefault("Y");
		pip1.setProductId(1);
		p1.setProductImgList(pip1);
		
		ProductImg pip2 = new ProductImg();
		pip2.setImgId(2);
		pip2.setImgName("orange2.jpg");
		pip2.setImgDefault("N");
		pip2.setProductId(1);
		p1.setProductImgList(pip2);
		ProductImg pip112 = new ProductImg();
		pip112.setImgId(2);
		pip112.setImgName("orange2.jpg");
		pip112.setImgDefault("N");
		pip112.setProductId(1);
		p1.setProductImgList(pip112);
		ProductImg pip113 = new ProductImg();
		pip113.setImgId(2);
		pip113.setImgName("orange2.jpg");
		pip113.setImgDefault("N");
		pip113.setProductId(1);
		p1.setProductImgList(pip113);
		
		list.add(p1);
		
		Product p2 = new Product();
		p2.setProductId(2);
		p2.setProductName("Apple Juice");
		p2.setProductDetail("Good test app!");
		
		ProductImg pip3 = new ProductImg();
		pip3.setImgId(3);
		pip3.setImgName("app1.jpg");
		pip3.setImgDefault("Y");
		pip3.setProductId(2);
		p2.setProductImgList(pip3);
		
		ProductImg pip4 = new ProductImg();
		pip4.setImgId(4);
		pip4.setImgName("app2.jpg");
		pip4.setImgDefault("N");
		pip4.setProductId(2);
		p2.setProductImgList(pip4);
		
		list.add(p2);
		
		Product p3 = new Product();
		p3.setProductId(2);
		p3.setProductName("Apple Juice");
		p3.setProductDetail("Good test app!");
		
		ProductImg pip5 = new ProductImg();
		pip5.setImgId(3);
		pip5.setImgName("app1.jpg");
		pip5.setImgDefault("Y");
		pip5.setProductId(2);
		p3.setProductImgList(pip5);
		
		ProductImg pip6 = new ProductImg();
		pip6.setImgId(4);
		pip6.setImgName("app2.jpg");
		pip6.setImgDefault("N");
		pip6.setProductId(2);
		p3.setProductImgList(pip5);
		
		list.add(p3);
		
		Product p4 = new Product();
		p4.setProductId(2);
		p4.setProductName("Apple Juice");
		p4.setProductDetail("Good test app!");
		
		ProductImg pip7 = new ProductImg();
		pip7.setImgId(3);
		pip7.setImgName("app1.jpg");
		pip7.setImgDefault("Y");
		pip7.setProductId(2);
		p4.setProductImgList(pip7);
		
		ProductImg pip8 = new ProductImg();
		pip8.setImgId(4);
		pip8.setImgName("app2.jpg");
		pip8.setImgDefault("N");
		pip8.setProductId(2);
		p4.setProductImgList(pip8);
		
		list.add(p4);
		
		Product p5 = new Product();
		p5.setProductId(2);
		p5.setProductName("Apple Juice");
		p5.setProductDetail("Good test app!");
		
		ProductImg pip9 = new ProductImg();
		pip9.setImgId(3);
		pip9.setImgName("app1.jpg");
		pip9.setImgDefault("Y");
		pip9.setProductId(2);
		p5.setProductImgList(pip9);
		
		ProductImg pip10 = new ProductImg();
		pip10.setImgId(4);
		pip10.setImgName("app2.jpg");
		pip10.setImgDefault("N");
		pip10.setProductId(2);
		p5.setProductImgList(pip10);
		
		list.add(p5);
		
		Product p6 = new Product();
		p6.setProductId(2);
		p6.setProductName("Apple Juice");
		p6.setProductDetail("Good test app!");
		
		ProductImg pip11 = new ProductImg();
		pip11.setImgId(3);
		pip11.setImgName("app1.jpg");
		pip11.setImgDefault("Y");
		pip11.setProductId(2);
		p6.setProductImgList(pip11);
		
		ProductImg pip12 = new ProductImg();
		pip12.setImgId(4);
		pip12.setImgName("app2.jpg");
		pip12.setImgDefault("N");
		pip12.setProductId(2);
		p6.setProductImgList(pip12);
		
		list.add(p6);
		
		Product p7 = new Product();
		p7.setProductId(1);
		p7.setProductName("Orange Juice");
		p7.setProductDetail("Good test!");
		
		ProductImg pip13 = new ProductImg();
		pip13.setImgId(1);
		pip13.setImgName("orange1.jpg");
		pip13.setImgDefault("Y");
		pip13.setProductId(1);
		p7.setProductImgList(pip13);
		
		ProductImg pip14 = new ProductImg();
		pip14.setImgId(2);
		pip14.setImgName("orange2.jpg");
		pip14.setImgDefault("N");
		pip14.setProductId(1);
		p7.setProductImgList(pip14);
		
		list.add(p7);
		
		JSONArray jsonObject = JSONArray.fromObject(list);
		out.print(jsonObject);
	}
	
	private void getProductDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
}
